package org.rcloud.io.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.rcloud.io.Utils.EndorsementVo;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.mapper.EndorsementMapper;
import org.rcloud.io.model.Endorsement;
import org.rcloud.io.service.EndorsementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/2/15.
 */
@Service(value = "EndorsementService")
public class EndorsementServiceImpl implements EndorsementService {

    @Autowired
    private EndorsementMapper endorsementMapper;

    @Override
    public ResultVo save(Endorsement endorsement) {

        if (endorsement.equals("") || endorsement == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                int i = endorsementMapper.insertSelective(endorsement);
                return ResultVo.setOK(i);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }


    /**
     * 视频转码
     *
     * @param ffmpegPath   转码工具的存放路径
     * @param absolutePath 用于指定要转换格式的文件,要截图的视频源文件
     * @param codcFilePath 格式转换后的的文件保存路径
     * @param mediaPicPath 截图保存路径
     * @return
     * @throws Exception
     */
    @Override
    public boolean executeCodecs(String ffmpegPath, String absolutePath, String codcFilePath, String mediaPicPath) {
        // 创建一个List集合来保存转换视频文件为flv格式的命令
        List<String> convert = new ArrayList<String>();
        convert.add(ffmpegPath); // 添加转换工具路径
        convert.add("-i"); // 添加参数＂-i＂，该参数指定要转换的文件
        convert.add(absolutePath); // 添加要转换格式的视频文件的路径
        convert.add("-qscale");     //指定转换的质量
        convert.add("6");
        convert.add("-ab");        //设置音频码率
        convert.add("64");
        convert.add("-ac");        //设置声道数
        convert.add("2");
        convert.add("-ar");        //设置声音的采样频率
        convert.add("22050");
        convert.add("-r");        //设置帧频
        convert.add("24");
        convert.add("-y"); // 添加参数＂-y＂，该参数指定将覆盖已存在的文件
        convert.add(codcFilePath);

        // 创建一个List集合来保存从视频中截取图片的命令
        List<String> cutpic = new ArrayList<String>();
        cutpic.add(ffmpegPath);
        cutpic.add("-i");
        cutpic.add(absolutePath); // 同上（指定的文件即可以是转换为flv格式之前的文件，也可以是转换的flv文件）
        cutpic.add("-y");
        cutpic.add("-f");
        cutpic.add("image2");
        cutpic.add("-ss"); // 添加参数＂-ss＂，该参数指定截取的起始时间
        cutpic.add("0"); // 添加起始时间为第0秒
        cutpic.add("-t"); // 添加参数＂-t＂，该参数指定持续时间
        cutpic.add("0.001"); // 添加持续时间为1毫秒
        cutpic.add("-s"); // 添加参数＂-s＂，该参数指定截取的图片大小
        cutpic.add("1280*720"); // 添加截取的图片大小为350*240
        cutpic.add(mediaPicPath); // 添加截取的图片的保存路径

        boolean mark = true;
        ProcessBuilder builder = new ProcessBuilder();
        try {
            builder.command(convert);
            builder.redirectErrorStream(true);
            builder.start();

            builder.command(cutpic);
            builder.redirectErrorStream(true);
            // 如果此属性为 true，则任何由通过此对象的 start() 方法启动的后续子进程生成的错误输出都将与标准输出合并，
            //因此两者均可使用 Process.getInputStream() 方法读取。这使得关联错误消息和相应的输出变得更容易
            builder.start();
        } catch (Exception e) {
            mark = false;
            System.out.println(e);
            e.printStackTrace();
        }
        return mark;
    }

    @Override
    public PageVo<Endorsement> findEndorsement() {

        try {
            List<Endorsement> list = endorsementMapper.selectAll();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo findEndorsementByNum(String eid) {

        if (eid.equals("") || eid == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                Endorsement endorsement = endorsementMapper.selectByNum(eid);
                return ResultVo.setOK(endorsement);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }

    }

    @Override
    public ResultVo updateArtid(String nameTitle, String shouming) {
        try {
            int i = endorsementMapper.updateByTitle(nameTitle, shouming);
            return ResultVo.setOK(i);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo findEndorsementById(int id) {
        try {
            Endorsement endorsement = endorsementMapper.selectByPrimaryKey(id);
            return ResultVo.setOK(endorsement);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo updateById(Endorsement endorsement) {

        if (endorsement.equals("") || endorsement == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                int i = endorsementMapper.updateByPrimaryKeySelective(endorsement);
                return ResultVo.setOK(i);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }

    }

    @Override
    public PageInfo<Endorsement> selectByOther(int pageNum, int pageSize, Map map) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Endorsement> endorsementList = endorsementMapper.selectByOther(map);
        PageInfo<Endorsement> pageInfo = new PageInfo<>(endorsementList);
        return pageInfo;
    }

    @Override
    public PageVo<Endorsement> findEndorsementByDQ() {
        try {
            List<Endorsement> list = endorsementMapper.selectDQ();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public PageVo<Endorsement> findEndorsementByWS() {

        try {
            List<Endorsement> list = endorsementMapper.selectWS();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public PageVo<Endorsement> findEndorsementByXD() {

        try {
            List<Endorsement> list = endorsementMapper.selectXD();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }

    }

    @Override
    public PageVo<Endorsement> findEndorsementByXK() {

        try {
            List<Endorsement> list = endorsementMapper.selectXK();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }

    }

    @Override
    public PageVo<Endorsement> findEndorsementByEtype(String etype) {

        if (etype.equals("") || etype == null) {
            return PageVo.setError("传入数据为空");
        } else {
            try {
                List<Endorsement> list = endorsementMapper.selectByEtype(etype);
                return PageVo.setOk(list);
            } catch (Exception e) {
                e.printStackTrace();
                return PageVo.setError(e.getMessage());
            }
        }

    }

    @Override
    public PageVo<Endorsement> findEndorsementByName(String name) {

        try {
            List<Endorsement> list = endorsementMapper.selectByName(name);
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }

    }

    @Override
    public PageVo<Endorsement> findEndorsementByValue(String params) {

        if (params.equals("") || params == null) {
            return PageVo.setError("传入数据为空");
        } else {
            try {
                List<Endorsement> list = endorsementMapper.selectByValue(params);
                return PageVo.setOk(list);
            } catch (Exception e) {
                e.printStackTrace();
                return PageVo.setError(e.getMessage());
            }
        }

    }

    @Override
    public ResultVo selectByendorsementid(String params) {

        if (params.equals("") || params == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                Endorsement endorsement = endorsementMapper.selectByendorsementid(params);
                return ResultVo.setOK(endorsement);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }

    }

    @Override
    public PageInfo<Endorsement> selectByOthers(int pageNum, int pageSize, Map map) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Endorsement> endorsementList = endorsementMapper.selectByOthers(map);
        PageInfo<Endorsement> pageInfo = new PageInfo<>(endorsementList);
        return pageInfo;
    }

    @Override
    public PageInfo<EndorsementVo> findByCondition(int pageNum, int pageSize, Endorsement endorsement) {
        String name = endorsement.getName();
        if (name != null && !name.equals("")){
             String s = "%" + endorsement.getName() + "%";
            endorsement.setName(s);
        }
        if (endorsement.getEtype() != null && !endorsement.getEtype().equals("")){
            String s = "%" + endorsement.getEtype() + "%";
            endorsement.setEtype(s);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<EndorsementVo> stu=endorsementMapper.selectByCondition(endorsement);
        PageInfo<EndorsementVo> page = new PageInfo<>(stu); //自动封装总数count以及分页,数据返回页面
        return page;


    }

    @Override
    public ResultVo randomShow() {


        try {
            //修改原随机展示36户flag
            endorsementMapper.cancelShow();

            //获取新的随机展示36户
            List<Endorsement> list = endorsementMapper.random();

            //循环修改新的随机展示36户flag
            for (Endorsement endorsement: list) {
                endorsementMapper.addShow(endorsement);
            }
            return ResultVo.setOK(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }


}
